package com.example.meituan.activity;

import static com.example.meituan.utils.Constant.WEB_BASE;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.meituan.R;
import com.example.meituan.bean.User;
import com.example.meituan.service.UserService;
import com.example.meituan.utils.UserResponse;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RegisterActivity extends AppCompatActivity {

    private EditText editTextUsername, editTextPassword,editTextPassword1;
    private Button buttonRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        getSupportActionBar().hide();//隐藏标题栏
        editTextUsername = findViewById(R.id.editTextUsername);
        editTextPassword = findViewById(R.id.editTextPassword);
        editTextPassword1 = findViewById(R.id.editTextPassword1);
        buttonRegister = findViewById(R.id.buttonRegister);
        editTextUsername.setHint( "账号/手机号/邮箱");
        editTextPassword.setHint("密码");
        editTextPassword1.setHint("再次输入密码");
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(WEB_BASE)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        UserService userService = retrofit.create(UserService.class);
        buttonRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = editTextUsername.getText().toString();
                String password = editTextPassword.getText().toString();
                String password1 = editTextPassword1.getText().toString();
                if(!password.equals(password1)){
                    Toast.makeText(RegisterActivity.this, "两次密码不一致", Toast.LENGTH_SHORT).show();
                    return;
                }

                // 创建一个用户对象
                User user = new User(username, password);
                //
                // 发起注册请求
                Call<UserResponse> call = userService.registerUser(user);
                Log.i(USAGE_STATS_SERVICE, user.getPassword());
                call.enqueue(new Callback<UserResponse>() {
                    @Override
                    public void onResponse(Call<UserResponse> call, Response<UserResponse> response) {
                        if (response.isSuccessful()&& response.body() != null) {
                            UserResponse userResponse = response.body();
                            int code = userResponse.getCode();
                            if(code==0){
                                Toast.makeText(RegisterActivity.this, "已存在此用户名", Toast.LENGTH_SHORT).show();
                            }
                            // 注册成功后执行相应操作，比如跳转到登录页面
                            Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                            startActivity(intent);
                            finish();
                        } else {
                            Toast.makeText(RegisterActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
                        }
                    }
                    @Override
                    public void onFailure(Call<UserResponse> call, Throwable t) {
                        Log.e("Retrofit", "请求失败: " + t.getMessage());
                        // 处理API调用失败
                        Toast.makeText(RegisterActivity.this, "连接服务器失败", Toast.LENGTH_SHORT).show();
                    }

                });
            }
        });

    }
    protected long exitTime;//记录第一次点击时的时间
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
            startActivity(intent);
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
